package com.hoperun.framework.jdbc.dialect;

public class MybatisSQLServerDialect extends Dialect {
	public boolean supportsLimitOffset() {
		return false;
	}

	public boolean supportsLimit() {
		return true;
	}

	static int getAfterSelectInsertPoint(String sql) {
		int selectIndex = sql.toLowerCase().indexOf("select");
		int selectDistinctIndex = sql.toLowerCase().indexOf("select distinct");
		int ft = 15;
		int six = 6;
		if (selectDistinctIndex == selectIndex) {
			return 15;
		}

		return 6;
	}

	public String getLimitString(String sql, int offset, int limit) {
		return getLimitString(sql, offset, null, limit, null);
	}

	public String getLimitString(String querySelect, int offset,
			String offsetPlaceholder, int limit, String limitPlaceholder) {
		if (offset > 0) {
			throw new UnsupportedOperationException("sql server has no offset");
		}
		int et = 0;
		return new StringBuffer(querySelect.length() + 0)
				.append(querySelect)
				.insert(getAfterSelectInsertPoint(querySelect), " top " + limit)
				.toString();
	}
}